package gui;

import java.awt.*;
import java.security.*;

import javax.swing.JFrame;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JRootPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;

import businessobjects.Test;
import businessobjects.User;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import logik.UserManagement;

public class Login {

	private JFrame frame;
	private JTextField textField;
	private JPasswordField passwordField;
	UserManagement um = new UserManagement();
	private User user = null;
	private Test test = null;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Login window = new Login();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Login() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);

		JLabel lblLogin = new JLabel("Login");
		lblLogin.setFont(new Font("Tahoma", Font.PLAIN, 18));
		lblLogin.setBounds(10, 11, 93, 35);
		frame.getContentPane().add(lblLogin);

		textField = new JTextField();
		textField.setBounds(104, 75, 86, 20);
		frame.getContentPane().add(textField);
		textField.setColumns(10);

		passwordField = new JPasswordField();
		passwordField.setBounds(104, 106, 86, 20);
		frame.getContentPane().add(passwordField);

		JLabel lblUsername = new JLabel("Username");
		lblUsername.setBounds(10, 78, 69, 14);
		frame.getContentPane().add(lblUsername);

		JLabel lblPassword = new JLabel("Password");
		lblPassword.setBounds(10, 109, 69, 14);
		frame.getContentPane().add(lblPassword);

		JButton btnLogin = new JButton("Login");
		btnLogin.addActionListener(new ActionListener() {
			@SuppressWarnings("deprecation")
			public void actionPerformed(ActionEvent arg0) {
				User user = new User(textField.getText(), passwordField.getText());
				/*user.setUsername(textField.getText());
				user.setPasswort(passwordField.getText());*/
				if (um.login(user) == true) {
					System.out.println("Login successful");
					frame.setVisible(false);
					test = new Test();
					mathKwiz mt = new mathKwiz(user, test);
				} else {
					System.out.println("Login failed");
					System.out.println(user.getUsername() + " " + user.getPasswort());
				}
			}
		});
		
		JButton btnNewUser = new JButton("Neuer Benutzer");
		btnNewUser.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				frame.setVisible(false);
				userNew un = new userNew();			
				
			}
			
			
		});
		
		JRootPane pane1 = frame.getRootPane(); // Mit Enter weiter gelangen//
		pane1.setDefaultButton(btnLogin); // Mit Enter weiter gelangen//
		btnLogin.setBounds(101, 154, 89, 23);
		btnNewUser.setBounds(101, 184, 130, 23);
		frame.getContentPane().add(btnLogin);
		frame.getContentPane().add(btnNewUser);
	}
}
